package longrise.phone.com.bjjt_jyb.compensate;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.Constant.URLConstant;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.CommontUtils;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.Utils.UiUtils;
import longrise.phone.com.bjjt_jyb.widget.AlertDialogRecordSec;


/**
 * 有争议，无争议选择页面，Created by Administrator on 2016/6/2.
 */
public class DisputeView extends LFView implements View.OnClickListener {

    private Context context = null;
    private View view = null;
    private BaseApplication mApplication;
    private LinearLayout hn_btn_back;
    private String appCaseno;
    private Button bt_disputereport;
    private Button bt_disputereport2;
    private TextView tv_disputereport1;
    private TextView tv_disputereport2;
    private TextView tv_disputereport3;
    private TextView tv_disputereport4;
    private TextView tv_disputereport5;
    private TextView tv_disputereport6;
    private RadioGroup rg_createdispute1;
    private RadioButton rb_createdispute1;
    private RadioButton rb_createdispute2;
    private RadioButton rb_createdispute3;
    private RadioGroup rg_createdispute2;
    private RadioButton rb_createdispute4;
    private RadioButton rb_createdispute5;
    private RadioButton rb_createdispute6;
    private Dialog processDialog;
    private EntityBean[] infoBean;
    private String hisDuty;
    private String myDuty;
    private Button bt_disputereport_xc;
    private String kckpUserType;
    private TextView rg_chooseartj;
    private TextView rg_choosearty;
    private LinearLayout ll_chooseartj;
    private LinearLayout ll_choosearty;
    private String valuej;
    private String valuey;
    private Integer fromwhere = 0;

    public DisputeView(Context c, EntityBean[] infoBean) {
        super(c);
        this.context = c;
        this.mApplication = (BaseApplication) c.getApplicationContext();
        this.infoBean = infoBean;

    }

    public DisputeView(Context c, EntityBean[] infoBean, Integer fromwhere) {
        super(c);
        this.context = c;
        this.mApplication = (BaseApplication) c.getApplicationContext();
        this.infoBean = infoBean;
        this.fromwhere = fromwhere;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_dispute, null);
        bt_disputereport = (Button) view.findViewById(R.id.bt_disputereport);
        bt_disputereport2 = (Button) view.findViewById(R.id.bt_disputereport2);
        bt_disputereport_xc = (Button) view.findViewById(R.id.bt_disputereport_xc);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        LinearLayout ll_choose1 = (LinearLayout) view.findViewById(R.id.ll_choose1);
        LinearLayout ll_choose2 = (LinearLayout) view.findViewById(R.id.ll_choose2);
        tv_disputereport1 = (TextView) view.findViewById(R.id.tv_disputereport1);//对方姓名
        tv_disputereport2 = (TextView) view.findViewById(R.id.tv_disputereport2);//车牌号
        tv_disputereport3 = (TextView) view.findViewById(R.id.tv_disputereport3);//电话
        TextView tv_new1 = (TextView) view.findViewById(R.id.tv_new1);
        TextView tv_new2 = (TextView) view.findViewById(R.id.tv_new2);
        kckpUserType = mApplication.getKckpUserType();
        if (null != kckpUserType && "9".equals(kckpUserType)) {
            bt_disputereport2.setVisibility(View.GONE);//交警没有远程定责
            ll_choose1.setVisibility(View.VISIBLE);
            ll_choose2.setVisibility(View.VISIBLE);
        } else {
            bt_disputereport_xc.setVisibility(View.GONE);//协警没有现场定责
            ll_choose1.setVisibility(View.GONE);
            ll_choose2.setVisibility(View.GONE);
        }
        tv_disputereport1.setText(infoBean[0].getString("carownname"));
        tv_disputereport3.setText(infoBean[0].getString("casecarphone"));
        rg_createdispute1 = (RadioGroup) view.findViewById(R.id.rg_createdispute1);
        rb_createdispute1 = (RadioButton) view.findViewById(R.id.rb_createdispute1);
        rb_createdispute2 = (RadioButton) view.findViewById(R.id.rb_createdispute2);
        rb_createdispute3 = (RadioButton) view.findViewById(R.id.rb_createdispute3);
        //rb_createdispute7 = (RadioButton) view.findViewById(R.id.rb_createdispute7);
        //rb_createdispute8 = (RadioButton) view.findViewById(R.id.rb_createdispute8);
        tv_disputereport4 = (TextView) view.findViewById(R.id.tv_disputereport4);//本方姓名
        tv_disputereport5 = (TextView) view.findViewById(R.id.tv_disputereport5);//车牌号
        tv_disputereport6 = (TextView) view.findViewById(R.id.tv_disputereport6);//电
        tv_disputereport4.setText(infoBean[1].getString("carownname"));
        if (fromwhere == 0) {
            String casecarnoJ = infoBean[0].getString("casecarno").toUpperCase();
            tv_disputereport2.setText(casecarnoJ);
            String casecarnoY = infoBean[1].getString("casecarno").toUpperCase();
            tv_disputereport5.setText(casecarnoY);
        } else if (fromwhere == 3) {//机动车与非机动车
            tv_new2.setText("车辆品牌");
            String casecarnoJ = infoBean[0].getString("casecarno").toUpperCase();
            tv_disputereport2.setText(casecarnoJ);
            tv_disputereport5.setText(infoBean[1].getString("frameno"));
        } else {//非机动车与非机动车都是用的车架号当做车辆品牌 4
            tv_new1.setText("车辆品牌");
            tv_new2.setText("车辆品牌");
            tv_disputereport2.setText(infoBean[0].getString("frameno"));
            tv_disputereport5.setText(infoBean[1].getString("frameno"));
        }
        this.appCaseno = infoBean[1].getString("appCaseNo");
        tv_disputereport6.setText(infoBean[1].getString("casecarphone"));
        rg_createdispute2 = (RadioGroup) view.findViewById(R.id.rg_createdispute2);
        rb_createdispute4 = (RadioButton) view.findViewById(R.id.rb_createdispute4);
        rb_createdispute5 = (RadioButton) view.findViewById(R.id.rb_createdispute5);
        rb_createdispute6 = (RadioButton) view.findViewById(R.id.rb_createdispute6);
        //rb_createdispute9 = (RadioButton) view.findViewById(R.id.rb_createdispute9);
        //rb_createdispute10 = (RadioButton) view.findViewById(R.id.rb_createdispute10);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("责任认定");
        rg_chooseartj = (TextView) view.findViewById(R.id.rg_chooseartj);//甲方选择违法条款
        ll_chooseartj = (LinearLayout) view.findViewById(R.id.ll_chooseartj);
        rg_choosearty = (TextView) view.findViewById(R.id.rg_choosearty);//乙方选择违法条款
        ll_choosearty = (LinearLayout) view.findViewById(R.id.ll_choosearty);
    }

    private void regEvent(boolean b) {
        if (null != bt_disputereport) {
            bt_disputereport.setOnClickListener(b ? this : null);
        }
        if (null != bt_disputereport2) {
            bt_disputereport2.setOnClickListener(b ? this : null);
        }
        if (null != bt_disputereport_xc) {
            bt_disputereport_xc.setOnClickListener(b ? this : null);
        }

        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
        if (null != rg_chooseartj) {
            rg_chooseartj.setOnClickListener(b ? this : null);
        }
        if (null != rg_choosearty) {
            rg_choosearty.setOnClickListener(b ? this : null);
        }
        //对方责任选项
        if (null != rg_createdispute1) {
            rg_createdispute1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.rb_createdispute1:
                            hisDuty = "0";//全责
                            rb_createdispute5.setChecked(true);
                            ll_chooseartj.setVisibility(View.VISIBLE);
                            break;
                        case R.id.rb_createdispute2://无责
                            hisDuty = "1";
                            rb_createdispute4.setChecked(true);
                            ll_chooseartj.setVisibility(View.GONE);
                            rg_chooseartj.setText("请选择违法条款");
                            valuej = "";
                            break;
                        case R.id.rb_createdispute3://同责
                            hisDuty = "2";
                            rb_createdispute6.setChecked(true);
                            ll_chooseartj.setVisibility(View.VISIBLE);
                            break;
                       /* case R.id.rb_createdispute7://主责
                            hisDuty = "3";
                            rb_createdispute10.setChecked(true);
                            break;
                        case R.id.rb_createdispute8://次责
                            hisDuty = "4";
                            rb_createdispute9.setChecked(true);
                            break;*/
                    }
                }
            });
        }
        //本方责任选项
        if (null != rg_createdispute2) {
            rg_createdispute2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.rb_createdispute4:
                            myDuty = "0";
                            rb_createdispute2.setChecked(true);
                            ll_choosearty.setVisibility(View.VISIBLE);
                            break;
                        case R.id.rb_createdispute5:
                            myDuty = "1";
                            rb_createdispute1.setChecked(true);
                            ll_choosearty.setVisibility(View.GONE);
                            rg_choosearty.setText("请选择违法条款");
                            valuey = "";
                            break;
                        case R.id.rb_createdispute6:
                            myDuty = "2";
                            rb_createdispute3.setChecked(true);
                            ll_choosearty.setVisibility(View.VISIBLE);
                            break;
                        /*case R.id.rb_createdispute9:
                            myDuty = "3";
                            rb_createdispute8.setChecked(true);
                            break;
                        case R.id.rb_createdispute10:
                            myDuty = "4";
                            rb_createdispute7.setChecked(true);
                            break;*/
                    }
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        AlertDialogRecordSec alertdialogrecordsec;
        AlertDialogRecordSec alertdialogrecordsec2;
        switch (v.getId()) {
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
            case R.id.rg_chooseartj:
                alertdialogrecordsec = new AlertDialogRecordSec(context).builder();
                alertdialogrecordsec.setOnitemsel(new AlertDialogRecordSec.onitemsecl() {
                    @Override
                    public void onItemsec(String text, String i) {
                        rg_chooseartj.setText(text);
                        valuej = i;
                    }
                });
                alertdialogrecordsec.show();
                break;
            case R.id.rg_choosearty:
                alertdialogrecordsec2 = new AlertDialogRecordSec(context).builder();
                alertdialogrecordsec2.setOnitemsel(new AlertDialogRecordSec.onitemsecl() {
                    @Override
                    public void onItemsec(String text, String i) {
                        rg_choosearty.setText(text);
                        valuey = i;//法律条款对应的值
                    }
                });
                alertdialogrecordsec2.show();
                break;
            case R.id.bt_disputereport_xc:
                //现场定责
                if (null != kckpUserType && "9".equals(kckpUserType)) {
                    if (null == hisDuty) {
                        UiUtil.showToast(context, "请选择责任类型");
                        return;
                    }
                    if (!"1".equals(hisDuty)) {
                        if (TextUtils.isEmpty(valuej)) {
                            UiUtil.showToast(context, "请选择甲方违法条款");
                            return;
                        }
                    }
                    if (!"1".equals(myDuty)) {
                        if (TextUtils.isEmpty(valuey)) {
                            UiUtil.showToast(context, "请选择乙方违法条款");
                            return;
                        }
                    }
                }
                UiUtils.showDialog(context, null, null, "确定提交吗？", "返回", "确认",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                submitAccidentCase(3);//现场定责
                            }
                        });
                break;
            case R.id.bt_disputereport:
                //无争议，自己定责，事故上传成功时生成协议书
                if (null != kckpUserType && "9".equals(kckpUserType)) {
                    if (null == hisDuty) {
                        UiUtil.showToast(context, "请选择责任类型");
                        return;
                    }
                }
                //弹出dialog，二次确认
                //弹出照片上传正常的dialog
                UiUtils.showDialog(context, null, null, "确定提交吗？", "返回", "确认",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                submitAccidentCase(1);//无争议
                            }
                        });
                break;
            case R.id.bt_disputereport2:
                //有争议交警定责
                if (null != kckpUserType && "9".equals(kckpUserType)) {
                    if (null == hisDuty) {
                        UiUtil.showToast(context, "请选择责任类型");
                        return;
                    }
                }
                UiUtils.showDialog(context, null, null, "确定提交吗？", "返回", "确认",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                submitAccidentCase(2);//远程定责
                            }
                        });
                break;

        }
    }

    private void createAgreementByHand() {
        EntityBean hisBean = new EntityBean();
        hisBean.set("ownother", "1");
        hisBean.set("carownphone", tv_disputereport3.getText().toString().trim());
        hisBean.set("dutytype", hisDuty);
        hisBean.set("carownname", tv_disputereport1.getText().toString().trim());
        hisBean.set("casecarno", tv_disputereport2.getText().toString().trim().toUpperCase());

        EntityBean myBean = new EntityBean();
        myBean.set("ownother", "0");
        myBean.set("carownphone", tv_disputereport6.getText().toString().trim());
        myBean.set("dutytype", myDuty);
        myBean.set("carownname", tv_disputereport4.getText().toString().trim());
        myBean.set("casecarno", tv_disputereport5.getText().toString().trim().toUpperCase());
        EntityBean[] accidentBean = new EntityBean[]{hisBean, myBean};
        if (fromwhere == 3) {
            CreateAgreementView createagreementview = new CreateAgreementView(context, accidentBean, appCaseno, 3);
            FrameworkManager.getInstance().showNewForm(context, createagreementview);
        } else if (fromwhere == 4) {
            CreateAgreementView createagreementview = new CreateAgreementView(context, accidentBean, appCaseno, 4);
            FrameworkManager.getInstance().showNewForm(context, createagreementview);
        } else {
            CreateAgreementView createagreementview = new CreateAgreementView(context, accidentBean, appCaseno, 0);
            FrameworkManager.getInstance().showNewForm(context, createagreementview);
        }
    }

    //提交数据
    private void submitAccidentCase(final int createType) {
        processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
        EntityBean bean = mApplication.getKckpRequestBean();
        final String myCarNo = tv_disputereport5.getText().toString().trim().toUpperCase();
        // caseno
        bean.set("appcaseno", appCaseno);
        bean.set("casecarno", myCarNo);
        String phoneNumber = tv_disputereport6.getText().toString().trim();
        bean.set("appphone", phoneNumber);
        // 经度
        bean.set("caselon", mApplication.getLongitude());
        // 纬度
        bean.set("caselat", mApplication.getLatitude());
        bean.set("casehaplace", mApplication.getAddress());
        // 地址
        bean.set("accidentplace", infoBean[0].getString("accidentplace"));//事故地点
        // 提交时间
        bean.set("sibmitdate", CommontUtils.getNowTime());
        bean.set("areaid", mApplication.getAreaid()); // 区域
        //bean.set("areaid", "410101000000000000"); // 调试数据
        bean.set("alarmtime", CommontUtils.getNowTime());
        bean.set("token", mApplication.getToken());
        bean.set("accidenttype", infoBean[1].getString("accidentType"));
        bean.set("accidentdes", infoBean[1].getString("discription"));
        bean.set("disposetype", createType + "");//增加一个自己还是交警定责的类型字段
        String myCarType = infoBean[1].getString("carType");
        EntityBean[] casecarlist;//车主信息数组
        EntityBean bfBean = new EntityBean();// 本方数据
        String myName = tv_disputereport4.getText().toString().trim(); // 车主姓名
        bfBean.set("carownname", myName);
        bfBean.set("carownphone", phoneNumber); //手机号
        bfBean.set("driverlicence", infoBean[1].getString("casecarId"));
        //车牌号转为大写
        String carnoYF = tv_disputereport5.getText().toString().trim().toUpperCase();
        bfBean.set("casecarno", carnoYF); // 案件车牌号
        bfBean.set("cartype", myCarType); // 车型
        bfBean.set("frameno", infoBean[1].getString("frameno"));//车架号
        bfBean.set("cardno", infoBean[1].getString("casecarId")); //身份证
        bfBean.set("trafficinsno", infoBean[1].getString("trafficinsno")); //保单号
        bfBean.set("drivernoimg", ""); //驾驶证照片
        bfBean.set("drivingnoimg", "");  //行驶证照片
        bfBean.set("party", "0");  //行驶证照片
        bfBean.set("legalabbreviation", valuey);  //乙方违法条款
        EntityBean dfBean = new EntityBean();// 对方数据
        dfBean.set("carownname", tv_disputereport1.getText().toString().trim()); // 车主姓名
        dfBean.set("carownphone", tv_disputereport3.getText().toString().trim());  // 电话
        //车牌号转为大写
        String carnoJF = tv_disputereport2.getText().toString().trim().toUpperCase();
        dfBean.set("casecarno", carnoJF);  // 案件车牌号
        dfBean.set("cartype", infoBean[0].getString("carType")); // 车型
        dfBean.set("frameno", infoBean[0].getString("frameno"));  //车架号
        dfBean.set("cardno", infoBean[0].getString("casecarId")); //身份证
        dfBean.set("trafficinsno", infoBean[0].getString("trafficinsno")); //保单号
        dfBean.set("drivernoimg", ""); //驾驶证照片
        dfBean.set("drivingnoimg", ""); //行驶证照片
        dfBean.set("driverlicence", infoBean[0].getString("casecarId"));
        dfBean.set("party", "1");//区分甲方乙方，甲方是1
        dfBean.set("legalabbreviation", valuej);//甲方违法条款
        if (createType == 1 || createType == 3) {
            bfBean.set("dutytype", myDuty);
            dfBean.set("dutytype", hisDuty);//当自己解决时增加一个责任类型的字段
        }
        casecarlist = new EntityBean[]{bfBean, dfBean};
        bean.set("casecarlist", casecarlist);
        LoadDataManagerCar.getInstance(context).callService(null,
                mApplication.getServerUrl(), URLConstant.HNJJAPPSUBMITCASEINFOR, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        EntityBean bean = (EntityBean) result;
                        if (null == bean) {
                            return;
                        }
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        if ("1".equals(restate)) {
                            UiUtil.showToast(context, "提交成功");
                            if (createType == 1 || createType == 3) {
                                //createType==1，自行解决进入生成协议书，createType==3交警现场定责
                                createAgreementByHand();
                            } else {
                                if (fromwhere == 3 || fromwhere == 4) {
                                    AccidentCheckView checkView = new AccidentCheckView(context, appCaseno, 3);
                                    FrameworkManager.getInstance().showNewForm(context, checkView);
                                } else {
                                    AccidentCheckView checkView = new AccidentCheckView(context, appCaseno, 2);
                                    FrameworkManager.getInstance().showNewForm(context, checkView);
                                }
                            }
                        } else {
                            showsingle(context, redes);
                        }

                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        UiUtil.showToast(context, "连接超时，请检查网络后重试。");

                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                    }
                });
    }

    private void showsingle(final Context context, String redes) {
        UiUtils.showSingleDialog(context, R.mipmap.a_32, redes, "确认", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeAllForm();
                OnDestroy();
                HomePageView homepageview = new HomePageView(context);
                FrameworkManager.getInstance().showNewForm(context, homepageview);
            }
        });
    }
}
